﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigEventCtrlViewModel : BaseViewModel<ConfigEventCtrlViewModel>
    {
       #region Variables

        private string _seachValue = "";

        private ObservableCollection<ConfigEventsModel> _disConfigEventModelList = new ObservableCollection<ConfigEventsModel>();

        private ConfigEventsModel _curConfigEvent;
        #endregion

        #region Constructor

        public ConfigEventCtrlViewModel()
        {
           }

        #endregion

        #region Properties
        public string SeachValue
        {
            get
            {
                return _seachValue;
            }
            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisConfigEventModelList");

            }
        }
        public ObservableCollection<ConfigEventsModel> DisConfigEventModelList
        {
            get
            {
                LikeFind();
                return _disConfigEventModelList;
            }

            set
            {
                _disConfigEventModelList = value;
                RaisePropertyChanged("DisConfigEventModelList");
            }
        }

        public ConfigEventsModel CurConfigEvent
        {
            get
            {
                return _curConfigEvent;
            }

            set
            {
                _curConfigEvent = value;
            }
        }

        private void LikeFind()
        {
            _disConfigEventModelList.Clear();
            List<ConfigEventsModel> lst = DataManager.Instance.ConfigEventsModelList.ToList().Where(O => O.Name.IsContain(_seachValue)).ToList();
            foreach (var item in lst)
            {
                item.PropertyChanged += Item_PropertyChanged;
                _disConfigEventModelList.Add(item);
            }
        }

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigEventsModel cem = sender as ConfigEventsModel;
            ConfigEventModelService.Instance.Update(cem);
            EventAndDrugWinViewModel.Instance.RefreshEventLeftItem();
        }
       
        #region Public/Protected Methods
        public void DelConfigEventsModel()
        {
            ConfigEventsModel configEventsModel = CurConfigEvent;
            if (configEventsModel != null)
            {
                if (configEventsModel.Code.Equals(Constants.EVENT_ANES) || configEventsModel.Code.Equals(Constants.EVENT_CTRL_REST) || configEventsModel.Code.Equals(Constants.EVENT_IN_ROOM) || configEventsModel.Code.Equals(Constants.EVENT_OPERATION) || configEventsModel.Code.Equals(Constants.EVENT_SUB_REST))
                {
                    MessageBoxCommon.ShowDialog("该事件不能删除！", MessageBoxCommon.OK);
                    return;
                }
                bool result= (bool)MessageBoxCommon.ShowDialog("确认删除此事件？", MessageBoxCommon.OKCANCLE);
                if (result)
                {
                    int flag = -1;
                    for (int i = 0; i < ConfigEventDataGridViewModel.Instance.ConfigEventModels.Count; i++)
                    {
                        if (ConfigEventDataGridViewModel.Instance.ConfigEventModels[i].Code.Equals(configEventsModel.Code))
                        {
                            flag = i;
                            break;
                        }
                    }
                    if (flag!=-1)
                    {
                        ConfigEventDataGridViewModel.Instance.ConfigEventModels.RemoveAt(flag);
                    }
                    int index = -1;
                    for (int i = 0; i < DataManager.Instance.ConfigEventsModelList.Count; i++)
                    {
                        if (DataManager.Instance.ConfigEventsModelList[i].Code.Equals(configEventsModel.Code))
                        {
                            index = i;
                            break;
                        }
                    }
                    if (index != -1)
                    {
                        DataManager.Instance.ConfigEventsModelList.RemoveAt(index);
                    }
                }
                RaisePropertyChanged("DisConfigEventModelList");
                ConfigEventModelService.Instance.DeleteById(configEventsModel.Id);
                EventAndDrugWinViewModel.Instance.RefreshEventLeftItem();
            }
        }

        public void AddRow()
        {
            ConfigEventsModel model = new ConfigEventsModel();
            model.Code = "event_code" + DateTime.Now.Ticks;
            model.Single = true;
            model.EventTypeCode = "eventType_2";
            DisConfigEventModelList.Insert(0, model);
            ConfigEventModelService.Instance.AddUpdate(model);
            DataManager.Instance.ConfigEventsModelList.Add(model);
            EventAndDrugWinViewModel.Instance.RefreshEventLeftItem();
            model.PropertyChanged += Item_PropertyChanged;
        }
        #endregion
        #endregion

        #region EventHandlers

        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion



    }

}
